package com.itheima.mm.controller;

import com.github.pagehelper.Page;
import com.itheima.mm.entity.Result;
import com.itheima.mm.pojo.Company;
import com.itheima.mm.service.CompanyService;
import com.itheima.mm.vo.CompanyVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description CompanyController
 * @Author xiao
 * @Date 2024-06-26
 */
@Slf4j
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @GetMapping("/findListAll.do")
    public Result<List<CompanyVO>> findListAll() {
        log.info("开始执行获取企业全部列表接口");
        List<CompanyVO> companyVOList = companyService.findListAll();

        return Result.success(companyVOList);
    }
    //启用禁用公司
    @PostMapping("/updateCompanyStatus")
    public ResponseEntity<Map<String, Object>> updateCompanyStatus(@RequestBody Map<String, Object> payload) {
        Integer id = (Integer) payload.get("id");
        Integer state = (Integer) payload.get("state");

        boolean success = companyService.updateCompanyStatus(id, state);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "状态更新成功" : "状态更新失败");

        return ResponseEntity.ok(response);
    }

//    企业分页查询
@GetMapping("/findAllCompanies")
public ResponseEntity<Map<String, Object>> findAllCompanies(
        @RequestParam(defaultValue = "1") int pageNum,
        @RequestParam(defaultValue = "10") int pageSize,
        @RequestParam(required = false) String shortName,
        @RequestParam(required = false) Integer isDis) {

    // 创建返回结果对象
    Map<String, Object> result = new HashMap<>();

    // 获取分页数据
    Page<Company> page = companyService.findAllCompanies(pageNum, pageSize, shortName, isDis);

    // 设置结果
    result.put("total", page.getTotal());
    result.put("result", page.getResult());

    return ResponseEntity.ok(result);
}

//    删除企业
@PostMapping("/delete")
public Map<String, Object> deleteCompany(@RequestParam int id) {
    Map<String, Object> response = new HashMap<>();
    try {
        companyService.deleteCompany(id);
        response.put("success", true);
    } catch (Exception e) {
        log.error("删除失败: ", e);
        response.put("success", false);
        response.put("message", e.getMessage());
    }
    return response;
}
//    新增企业
@PostMapping("/add")
public Map<String, Object> addCompany(@RequestBody Company company) {
    Map<String, Object> response = new HashMap<>();
    try {
        log.info("Received company: {}", company);
        companyService.addCompany(company);
        response.put("success", true);
    } catch (Exception e) {
        log.error("添加企业失败: ", e);
        response.put("success", false);
        response.put("message", e.getMessage());
    }
    return response;
}
}
